package com.waimai.www.service;



import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;

import com.waimai.www.dao.OrdersMapper;
import com.waimai.www.po.Orders;
import com.waimai.www.po.Caipin;
import com.waimai.www.service.OrdersService;


@Service
public class OrdersService {
	
	
	@Autowired OrdersMapper ordersMapper ;
	
	public Orders findById(Integer orderId) {
		
		return ordersMapper.findById(orderId);
		
	}    
		//根据订单获取订单明细
		public List<Orders> getOrdersById(Integer ordersId) throws SQLException {
			// TODO Auto-generated method stub
			CaipinService caipinService = new CaipinService();
			//根据订单id拿到订单明细
			List<Orders> ordersList = ordersMapper.getOrdersById(ordersId);
			for(int i=0;i<ordersList.size();i++){
				String fname = ordersList.get(i).getFname();
				Caipin caipin= caipinService.getCaipinById(fname.toString());
				ordersList.get(i).setCaipin(caipin);	
			}
			return ordersList;
		}
	
		
	    //保存订单以及订单明细信息
		public void saveOrdersAndOrders(Orders orders, List<Orders> orderList) throws SQLException {
			// TODO Auto-generated method stub
			
			ordersMapper.saveOrdersAndOrders(orders, orderList);
		}
		
		//根据用户id查找用户订单信息
		public List<Orders> getAllOrdersByUsersId(Integer usersId) throws SQLException {
			// TODO Auto-generated method stub
			try {
				return ordersMapper.getAllOrdersByUsersId(usersId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new SQLException(e);
			}
		}
}
